package carlyang;

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.file.Path;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.text.SimpleDateFormat;
import java.util.Date;

public class SystemOutToFile {
	public static OutputStream outputStream = null;
	public static PrintStream originalPrintStream = null;
	public static PrintStream printStream = null;
	public static SimpleDateFormat myDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");
	private SystemOutToFile() {
		// TODO Auto-generated constructor stub

	}

	public static void start(Path path) {
		try {
			outputStream = Files.newOutputStream(path,
					StandardOpenOption.CREATE, StandardOpenOption.APPEND);
			printStream = new PrintStream(outputStream, true);
			originalPrintStream = System.out; //��Ҫ�ȱ��棬֮�����û�System.out�����ڿ���̨�д�ӡ
			System.setOut(printStream);
			System.out.println(myDateFormat.format(new Date())+" *************** System.out To File ***************");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void close() {
		System.setOut(originalPrintStream);
		originalPrintStream = null;
		if (outputStream != null)
			try {
				outputStream.close();
				outputStream = null;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if (printStream != null){
			printStream.close();
			printStream = null;
		}

	}

}
